package com.qingxin.park.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.MaterialType;
import com.qingxin.park.service.MaterialTypeService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;


/**
 * 物资类别管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/materialType")
public class MaterialTypeAction extends BaseAction {
	
	@Autowired
	private MaterialTypeService materialTypeService;
	
	/**
	 * 加载树形数据
	 * @param pdatatype
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/materialtree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object cropstree(String pdatatype, String pid){
		if(StringUtils.isEmpty(pid)){
			List<TreeNode> list = new ArrayList<TreeNode>();
			list.add(new TreeNode("0", "物资管理", "ROOT", null, true)); 
			return list;
			
		}else{
			return materialTypeService.getUITreeData(pid, false);
		}
	}
	
	/**
	 * 加载类别列表分页数据
	 * @param search
	 * @param leftdate
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String search,String leftdate, PageParam pageParam, HttpServletRequest request){
		return materialTypeService.list(pageParam,search,leftdate,getLoginUser(request),request);
	}
	
	/**
	 * 添加类别
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, String datatype, HttpServletRequest request){
		if(!"ROOT".equals(datatype) || StringUtils.isEmpty(leftdata)){
			model.put("errormessage", "参数错误");
			return "sys/error/error";
		}
		model.put("typeid", leftdata);
		MaterialType materialType = materialTypeService.get(leftdata);
		model.put("materialType", materialType);
		return "trace/material/materialleibie_add";
	}
	/**
	 * 保存类别信息
	 * @param category
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid MaterialType materialType ,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info =materialTypeService.add(materialType,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 修改类别
	 * @param model
	 * @param typeId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String typeid, HttpServletRequest request){
		MaterialType materialType = materialTypeService.get(typeid);
		model.put("materialType", materialType);
		return "trace/material/materialleibie_edit";
	}
	
	/**
	 * 修改保存
	 * @param materialType
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid MaterialType materialType, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return materialTypeService.update(materialType, getLoginUser(request), request);
	}
	
	/**
	 * 删除类别
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] typeid,HttpServletRequest request){
		if(typeid==null || typeid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return materialTypeService.delete(typeid, getLoginUser(request), request);
	}
	
	/**
	 * 判断物资类别名名称是否存在
	 * @param typeName
	 * @param typeid
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String typeName, String typeid){
		boolean existed = materialTypeService.isNameDuplicate(typeName, typeid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断物资类编号是否存在
	 * @param typeCode
	 * @param typeid
	 * @return
	 */
	@RequestMapping(value="/isCodeDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isCodeDuplicate(String typeCode, String typeid){
		boolean existed = materialTypeService.isCodeDuplicate(typeCode, typeid);
		return existed ? "1":"0";
	}
}
